package com.ziyun.erp.web.modules.goodsCraftPrice.controller;

import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsAttributeService;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import com.ziyun.erp.modules.goodsCraftPrice.dto.GoodsCraftPriceDto;
import com.ziyun.erp.modules.goodsCraftPrice.dto.GoodsTemplateOptionDto;
import com.ziyun.erp.modules.goodsCraftPrice.entity.GoodsCraftPriceEntity;
import com.ziyun.erp.modules.goodsCraftPrice.service.GoodsCraftPriceService;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 标品工艺价格表
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-10 10:43:12
 */
@RestController
@RequestMapping("goodsCraftPrice/goodscraftprice")
public class GoodsCraftPriceController {
    @Autowired
    private GoodsCraftPriceService goodsCraftPriceService;

    @Autowired
    private GoodsAttributeService goodsAttributeService;

    @Autowired
    private GoodsOptionService goodsOptionService;

    @Autowired
    private GoodsTemplateService goodsTemplateService;


    /**
     * 获取基础商品工艺价格
     */
    @RequestMapping("/getGoodsCraftPriceList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftPriceList(){
        //商户id 此处自营写死
        Long sellerId = 1L;

        Map<String ,Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsCraftPriceList(sellerId);

        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

    /**
     * 获取基础商品模板工艺价格
     */
    @RequestMapping("/getGoodsTemplateCraftPriceList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateCraftPriceList(){
        //商户id 此处自营写死
        Long sellerId = 1L;

        Map<String ,Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsTemplateCraftPriceList(sellerId);

        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

    /**
     * 获取基础商品模板工艺价格 弹窗用
     */
    @RequestMapping("/getGoodsTemplateCraftPriceWindowList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateCraftPriceWindowList(@RequestParam Integer templateId,@RequestParam String attributeIds){
        //商户id 此处自营写死
        Long sellerId = 1L;
        Map<String,Object> params = new HashMap<>();
        params.put("sellerId", sellerId);
        params.put("templateId", templateId);
        params.put("attributeIds", attributeIds);

        Map<String ,Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsTemplateCraftPriceWindowList(params);

        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

    /**
     * 获取基础商品工艺
     */
    @RequestMapping("/getGoodsCraftList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftList(){

        List<Map<String,Object>> goodsCraftList =   goodsAttributeService.getGoodsCraftList();

        return R.ok().put("goodsCraftList", goodsCraftList);
    }


    /**
     * 获取基础商品模板
     */
    @RequestMapping("/getGoodsTemplateList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateList(Integer attributeId){

        List<GoodsTemplateEntity> goodsTemplateList = this.goodsTemplateService.selectGoodsTemplateList(attributeId);

        return R.ok().put("goodsTemplateList", goodsTemplateList);
    }

    /**
     * 获取商品模板 关联的选项
     * @param goodsTemplateOptionDto
     * @return
     */
    @RequestMapping("/getGoodsTemplateOptionList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateOptionList(@RequestBody GoodsTemplateOptionDto goodsTemplateOptionDto){
        //商户id 此处自营写死
        Long sellerId = 1L;
        goodsTemplateOptionDto.setSellerId(sellerId);
        List<GoodsCraftPriceEntity> goodsCraftOptionPriceList = this.goodsCraftPriceService.getGoodsTemplateOptionList(goodsTemplateOptionDto);

        return R.ok().put("goodsCraftOptionPriceList", goodsCraftOptionPriceList);
    }

    /**
     * 获取基础商品工艺选项
     */
    @RequestMapping("/getGoodsCraftOptionList")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftOptionList(Integer attributeId){
        //商户id 此处自营写死
        Long sellerId = 1L;

        Map<String,Object> parasMap = new HashMap<>();
        parasMap.put("sellerId", sellerId);
        parasMap.put("attributeId", attributeId);

        List<GoodsCraftPriceEntity> goodsCraftOptionPriceList = goodsCraftPriceService.getGoodsCraftOptionPriceList(parasMap);


        return R.ok().put("goodsCraftOptionPriceList", goodsCraftOptionPriceList);
    }



    /**
     * 保存基础商品工艺价格
     */
    @RequestMapping("/save")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:save")
    public R save(@Valid @RequestBody GoodsCraftPriceDto goodsCraftPriceDto){
        // 获取当前登录人
        String userName = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        //商户id 此处自营写死
        Long sellerId = 1L;
        goodsCraftPriceService.save(goodsCraftPriceDto.getCraftPriceList(), userName, sellerId);
        return R.ok();
    }


    /**
     * 根据工艺删除
     */
    @RequestMapping("/deleteByAttributeId")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByAttributeId(Integer attributeId){
        //商户id 此处自营写死
        Long sellerId = 1L;

        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("attributeId", attributeId);
        parasMap.put("sellerId", sellerId);

        goodsCraftPriceService.deleteByAttributeId(parasMap);
        return R.ok();
    }

    /**
     * 根据工艺陪价删除
     */
    @RequestMapping("/deleteByIds")
    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByIds(@RequestBody List<Integer> ids){
        goodsCraftPriceService.deleteByIds(ids);
        return R.ok();
    }

}
